/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ChatTextSendEvent;
import de.lessvoid.nifty.controls.chatcontrol.ChatControl;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 * @date 2024年7月17日15点36分
 * @author JohnKkk
 */
public class NiftyGuiChatControl extends SimpleApplication implements ScreenController{

    private Nifty m_Nifty;
    private NiftyImage m_JohnKkkIcon;
    private NiftyImage m_EdIcon;
    
    public static void main(String[] args) {
        NiftyGuiChatControl niftyGuiLabel = new NiftyGuiChatControl();
        niftyGuiLabel.start();
    }
    
    @Override
    public void simpleInitApp() {
        // 设置默认背景色
        getViewPort().setBackgroundColor(ColorRGBA.DarkGray);
        
        // 初始化Nifty
        NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
                assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        m_Nifty = niftyDisplay.getNifty();
        // 将NiftyGUI显示对象添加到JME3中
        guiViewPort.addProcessor(niftyDisplay);
        
        m_Nifty.fromXml("Interface/NiftyGuiChatControl.xml", "GScreen0");
        
        // 禁用flyCam并显示鼠标
        flyCam.setEnabled(false);
        inputManager.setCursorVisible(true);
    }
    
    @NiftyEventSubscriber(id="chat0")
    public final void chatEvent(final String id, final ChatTextSendEvent chatTextSendEvent){
        // 我们这边假设操作的client就是JohnKkk,所以输入消息后在这里插入到聊天窗口显示
        chatTextSendEvent.getChatControl().receivedChatLine("   JohnKkk:" + chatTextSendEvent.getText(), m_JohnKkkIcon);
        // 同时往服务器发送这条JohnKkk的消息包...
    }
    
    @Override
    public void bind(Nifty nifty, Screen screen) {
        ChatControl chatControl = screen.findControl("chat0", ChatControl.class);
        // 添加两个用户到聊天用户面板
        m_JohnKkkIcon = nifty.getRenderEngine().createImage(screen, "Textures/chat-icon-user.png", false);
        chatControl.addPlayer("   JohnKkk", m_JohnKkkIcon);
        m_EdIcon = nifty.getRenderEngine().createImage(screen, "Textures/chat-icon-ninja.png", false);
        chatControl.addPlayer("   Ed", m_EdIcon);
        
        // 这里模拟从服务器收到Ed的消息,往聊天窗口插入Ed的消息
        chatControl.receivedChatLine("   Ed:Hello,John?", m_EdIcon);
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }
    
}
